﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HQHS.Library.DAO.Administrator
{
    public class AdministratorInfo
    {
        public int ID { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        //type=1 website menu, type=2 admin menu
        public byte UserType { get; set; }
        public bool? IsActive { get; set; }
        public byte? LoginStatus { get; set; }
        public DateTime? InsertedDate { get; set; }
        public DateTime? LastLoginDate { get; set; }
    }

    public class AdministratorInfoAssembler
    {
        private AdministratorInfoAssembler()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dr"></param>
        public static IList<AdministratorInfo> CreateList(SqlDataReader dr)
        {
            IList<AdministratorInfo> list = new List<AdministratorInfo>();

            if (dr != null)
            {
                // Reader ordinals
                int idOrd;
                int nameOrd;
                int passwordOrd;
                int typeOrd;
                int isActiveOrd;
                int loginStatusOrd;
                int insertedDateOrd;
                int lastLoginDateOrd;

                // Get ordinals
                idOrd = dr.GetOrdinal("ID");
                nameOrd = dr.GetOrdinal("UserName");
                passwordOrd = dr.GetOrdinal("Password");
                typeOrd = dr.GetOrdinal("UserType");
                isActiveOrd = dr.GetOrdinal("IsActive");
                loginStatusOrd = dr.GetOrdinal("LoginStatus");
                insertedDateOrd = dr.GetOrdinal("InsertedDate");
                lastLoginDateOrd = dr.GetOrdinal("LastLoginDate");

                // Read objects and add to list
                while (dr.Read())
                {
                    AdministratorInfo info = new AdministratorInfo();

                    info.ID = GetValue<int>(dr, idOrd, 0);
                    info.UserName = GetValue<string>(dr, nameOrd, null);
                    info.Password = GetValue<string>(dr, passwordOrd, null);
                    info.UserType = GetValue<byte>(dr, typeOrd, 1);
                    info.IsActive = GetValue<bool?>(dr, isActiveOrd, null);
                    info.LoginStatus = GetValue<byte?>(dr, loginStatusOrd, null);
                    info.InsertedDate = GetValue<DateTime?>(dr, insertedDateOrd, null);
                    info.LastLoginDate = GetValue<DateTime?>(dr, lastLoginDateOrd, null);

                    list.Add(info);
                }
            }

            return list;
        }

        private static TValue GetValue<TValue>(SqlDataReader dr, int ordinal, TValue dbNullValue)
        {
            TValue value;

            if (dr.IsDBNull(ordinal))
            {
                return dbNullValue;
            }
            else
            {
                value = (TValue)dr.GetValue(ordinal);

                if (value.GetType() == typeof(string))
                {
                    value = (TValue)RTrimString(value);
                }

                return value;
            }

        }

        private static object RTrimString(object value)
        {
            if (value != null)
            {
                return ((string)value).TrimEnd(' ');
            }
            else
            {
                return null;
            }
        }
    }
}
